(function($){
	var $here=0,$prev=0,$total=0,$eles=null,$showpage=1,$rand=0,$delay=5000,$speed=500,stop=0;
	function change(){
		if($prev!=$here){
			if($delay){
				clearTimeout(myTimeout);
			}
			if($showpage) {
				$('.slider #pages li').eq($prev).removeClass('active');
				$('.slider #pages li').eq($here).addClass('active');
			}
			fade();
			$prev = $here;
		}
	}
	function runSlideImgs(){
		if($rand==1){
			$here = rand(0,$total-1);
		} else {
			$here++;
			if($here == $total){
				$here = 0;
			}
		}
		if($showpage) {
			$('.slider #pages li').eq($prev).removeClass('active');
			$('.slider #pages li').eq($here).addClass('active');
		}
		fade();
		$prev = $here;
	}
	function fade(){
		/*stop = 1;
		$($eles[$prev]).fadeOut($speed/2);
		$($eles[$here]).find('#text').hide();
		$($eles[$here]).fadeIn($speed/2,function(){
			$(this).find('#text').fadeIn($speed/2,function(){
				stop = 0;
				if($delay){
					myTimeout = setTimeout(runSlideImgs,$delay);
				}
			});
		});*/
		left();
	}
	function left(){
		stop = 1;
		w = $($eles[$here]).width();
		$($eles[$prev]).animate({ 
			left:-w
		},{
			duration: $speed,
			easing:'easeOutCubic',
			queue: false,
			complete:function(){
				$(this).hide();
				stop=0;
				if($delay){
					myTimeout = setTimeout(runSlideImgs,$delay);
				}
			}
		});
		$($eles[$here]).css({
			left:w,
			display:'block'
		}).animate({ 
			left:0
		},{
			duration: $speed,
			easing:'easeOutCubic',
			queue: false
		});
		$('#text',$eles[$here]).css({
			right:-w,
		}).animate({ 
			right:(40)
		},{
			duration: $speed*2,
			easing:'easeOutCubic',
			queue: false
		});
	}
	function right(){
		stop = 1;
		$($eles[$prev]).animate({ 
			left:$($eles[$here]).width()
		},{
			duration: $speed,
			queue: false,
			complete:function(){
				$(this).hide();
				stop=0;
			}
		});
		$($eles[$here]).css({
			left:-$($eles[$here]).width(),
			display:'inline-block'
		}).animate({ 
			left:0
		},{
			duration: $speed,
			queue: false
		});
	}
	$(document).ready(function() {
		$eles = $('.slider #slide li');
		$total = $eles.length;
		if($total>1){
			resizeWindow();
			$(window).bind('resize',resizeWindow);
			$eles.each(function(i){
				var e = $(this);
				var img = $('img:first',e);
				e.css({
					'background':'url('+img.attr('src')+') no-repeat'
				});
				img.remove();
				if(i==0){
					$('#text',e).css({
						right:(40)
					});
				}else{
					e.hide();
				}
			});
			if($delay){
				myTimeout = setTimeout(runSlideImgs,$delay);
			}
			$('.slider #nav #next').click(function(e){
				e.preventDefault();
				if(stop==0){
					$prev = $here;
					$here++;
					if($here>=$total){
						$here = 0;
					}
					change();
				}
			});
			$('.slider #nav #prev').click(function(e){
				e.preventDefault();
				if(stop==0){
					$prev = $here;
					$here--;
					if($here<0){
						$here = $total-1;
					}
					change();
				}
			});
			if($showpage){
				$('.slider #pages li').each(function(i){
					$(this).click(function(e){
						e.preventDefault();
						if($prev!=i&&stop==0){
							$prev = $here;
							$here = i;
							change();
						}
					});
				});
			}
		}
	});
	function resizeWindow(){
		$window = $(window);
		if($('.slider').width()>$window.width())
		{
			$('.slider').css('width',$window.width());
			$('.slider #slide li').css('width',$window.width());
			$('.slider #nav').css({
				width:$window.width(),
				opacity:0.6
			});
		}
		
	}
})(jQuery);